package com.yixing.tech.common.base;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.yixing.tech.common.enums.ComparisonSymbol;
import com.yixing.tech.common.enums.YesOrNo;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.common.utils.CommonUtil;
import com.yixing.tech.common.utils.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yixing tech
 * at 2021/12/21
 * describe：
 */
@Data
@Accessors(chain=true)
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(allowGetters = true,value = {"createTime","updateTime"})
public class BaseVO{
    public String id;
    public String tenantId;
    public String createId;
    public String createName;
    @JsonIgnore
    public String updateId;
    @JsonIgnore
    public String updateName;
    public Date createTime;
    @JsonIgnore
    public Date updateTime;
    public String departmentId;
    @JsonIgnore
    public Integer isDelete = YesOrNo.NO.ordinal();

    private Map<String,List<? extends Object>> paramsCondition;

    public Sort sortCondition;
    public List<Sort> sortConditions;
    /**
     * 属性查询标记符
     */
    private Map<String,ComparisonSymbol> paramComparisonSymbols ;
    public void putParamsCondition(String key,List<? extends Object> list){
        getParamsCondition().put(key,list);
    }
    public void putComparisonSymbols(String attr,ComparisonSymbol symbol){
        getParamComparisonSymbols().put(attr,symbol);
    }

    @Data
    @Accessors(chain=true)
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Sort{
        public String code;
        public String direction;
    }


    /**
     * between action
     * @param key
     */
    public void actionTimeBetween(String key){
        if(paramsCondition !=null){
            List list = paramsCondition.get(key);
            if(!CommonUtil.listIsBlank(list)&&list.size()>1){
                try {
                    Class type = getClass().getField(key).getType();
                    if(type == Date.class){
                        String st = String.valueOf(list.get(0));
                        String et = String.valueOf(list.get(1));
                        list.set(0, DateUtil.praseDate(new SimpleDateFormat(
                                "yyyy/MM/dd HH:mm:ss"),st));
                        list.set(1,DateUtil.praseDate(new SimpleDateFormat(
                                "yyyy-MM-dd HH:mm:ss"),et+" 24:00:00"));
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
                putComparisonSymbols(key,ComparisonSymbol.between);
            }
        }
    }

    public Map<String, ComparisonSymbol> getParamComparisonSymbols() {
        if(paramComparisonSymbols == null)paramComparisonSymbols = new HashMap<>();
        return paramComparisonSymbols;
    }

    public Map<String,List<? extends Object>> getParamsCondition() {
        if(paramsCondition == null)paramsCondition = new HashMap<>();
        return paramsCondition;
    }
}
